"""
@Time : 2024/6/18 上午9:46
@Auth : AmirKD
@IDE : PyCharm
"""
import tkinter as tk
from tkinter import messagebox
import main


class LoginWindow:
    def __init__(self, root):
        self.root = root
        self.root.title("登录")

        # 设置窗口大小和居中显示
        window_width = 550
        window_height = 400
        screen_width = root.winfo_screenwidth()
        screen_height = root.winfo_screenheight()
        x_cordinate = int((screen_width / 2) - (window_width / 2))
        y_cordinate = int((screen_height / 2) - (window_height / 2))
        root.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))

        self.create_widgets()

    def create_widgets(self):
        self.welcome = tk.Label(self.root, text="欢迎使用项目管理工具",font=("Microsoft YaHei", 20),fg="red")
        self.username_label = tk.Label(self.root, text="用户名:", font=("Microsoft YaHei", 12))
        self.username_entry = tk.Entry(self.root, font=("Microsoft YaHei", 12))
        self.password_label = tk.Label(self.root, text="密码:", font=("Microsoft YaHei", 12))
        self.password_entry = tk.Entry(self.root, show="*", font=("Microsoft YaHei", 12))
        self.login_button = tk.Button(self.root, text="登录", command=self.login, font=("Microsoft YaHei", 15),bg="green",fg="black")
        self.cancel_button = tk.Button(self.root, text="取消", command=self.cancel, font=("Microsoft YaHei", 15),bg="red",fg="black")

        # 布局
        self.welcome.pack(pady=10)
        self.username_label.pack(pady=10)
        self.username_entry.pack(pady=10)
        self.password_label.pack(pady=10)
        self.password_entry.pack(pady=10)
        self.login_button.pack(side=tk.LEFT, padx=100, pady=50)
        self.cancel_button.pack(side=tk.RIGHT, padx=100, pady=50)

    def login(self):
        username = self.username_entry.get()
        password = self.password_entry.get()
        if username == "admin" and password == "123456":
            self.root.destroy()  # 关闭登录窗口
            main_root = tk.Tk()
            main.ProjectManagementTool(main_root)
            main_root.mainloop()
        else:
            messagebox.showerror("错误", "用户名或密码错误")

    def cancel(self):
        self.root.destroy()
